package club.dmyang.controller;

import club.dmyang.common.enums.RespCodeEnums;
import club.dmyang.common.vo.ResposeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 为了省事，就不写Service，dao了
 * Created by brss on 2018/8/17.
 */
@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/list")
    @ResponseBody
    public ResposeVo listAll() {
        List<Map<String, String>> resultList = new ArrayList<>();
        try {
            List<String> noticeList = stringRedisTemplate.opsForList().range("notice", 0, -1);
            for (int i = 0; i < noticeList.size(); i++) {
                Map<String, String> map = new LinkedHashMap<>();
                map.put("index", String.valueOf(i));
                map.put("content", noticeList.get(i));
                resultList.add(map);
            }
            return new ResposeVo(RespCodeEnums.RESP_CODE_SUCC.getCode(), "成功", resultList.size(), resultList);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResposeVo(RespCodeEnums.RESP_CODE_ERROR.getCode(), "失败");
        }

    }

    /**
     * 跳转到编辑页面（填充响应表单）
     *
     * @param index
     * @return
     */
    @RequestMapping("/edit/{index}")
    public String findOneByIndex(@PathVariable("index") Long index, Model model) {
        try {
            String notice = stringRedisTemplate.opsForList().index("notice", index);
            model.addAttribute("index", index);
            model.addAttribute("content", notice);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "admin/notice-add";
    }

    @ResponseBody
    @RequestMapping("/add")
    public ResposeVo addOne(String content) {
        try {
            stringRedisTemplate.opsForList().rightPush("notice", content);
            return new ResposeVo(RespCodeEnums.RESP_CODE_SUCC.getCode(), "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResposeVo(RespCodeEnums.RESP_CODE_ERROR.getCode(), "添加失败");
        }
    }

    /**
     * 安装内容删除
     *
     * @param notice
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete/{notice}")
    public ResposeVo delOne(@PathVariable("notice") String notice) {
        try {
            //??删除一个
            stringRedisTemplate.opsForList().remove("notice", 1, notice);
            return new ResposeVo(RespCodeEnums.RESP_CODE_SUCC.getCode(), "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResposeVo(RespCodeEnums.RESP_CODE_ERROR.getCode(), "删除失败");
        }
    }

    /**
     * 按照索引修改
     *
     * @param index
     * @param content
     * @return
     */
    @ResponseBody
    @RequestMapping("/update")
    public ResposeVo updateOne(Integer index, String content) {
        try {
            //??修改一个
            stringRedisTemplate.opsForList().set("notice", index, content);
            return new ResposeVo(RespCodeEnums.RESP_CODE_SUCC.getCode(), "更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResposeVo(RespCodeEnums.RESP_CODE_ERROR.getCode(), "更新失败");
        }
    }

}
